let app = getApp();
var httptool = require("../../utils/zry.httptool.js");
Page({
  data:{
      param:{},//获取好友信息参数
      personInfo:{},//好友信息
      forumParam:{},//获取帖子信息参数
      page: { PageIndex: 0, PageSize: 10 },
      topics:[],//好友帖子信息
      self:false,//判断id是否是本人，本人隐藏关注
      ConConfirm:'',
      delParam:{},//关注取消关注参数
      msgprop:'',//消息提示
      LikeTimes:''//点赞数量  
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    let that=this;
    let param={};
    let delParam={};
    param.ToUserId=options.ToUserId;//好友id
    delParam.ConcernId=options.ToUserId;
    console.log('option id是'+ param.ToUserId);
    param.UserId=app.getStorage('customerId');
    delParam.UserId=app.getStorage('customerId');
    that.setData({param:param,delParam:delParam});
    that.getFriendInfo();
    that.getFriendForum();
  },
  onPullDownRefresh: function () {//下拉刷新
    let that = this;
    let page = { PageIndex: 0, PageSize: 10 };
    that.setData({ topics: [], page: page });
    that.getFriendForum();
    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {//上拉加载
    let that = this;
    that.getFriendForum();
  },
  getFriendInfo:function(){//获取别人信息
    let that=this;
    httptool.ajaxPost("api/Customer/GetFriendInfo", that.data.param, function (res) {
        if (res.suc) {
            let data=res.data;
            if(data.ID==that.data.param.UserId){
                that.setData({self:true,ConConfirm:''});
            }else{
              that.setData({self:false});
            }
            if(data.IsConcern==true){
                if(data.ID!=that.data.param.UserId){
                  that.setData({ConConfirm:'取消关注'});
                } 
            }else{
              that.setData({ConConfirm:'关注'});
            }
            that.setData({personInfo:data});
        } else {
           app.showNotify(that, res.msg);
        }
    });
  },
  getFriendForum:function(){//获取好友帖子
    let that=this;
    let forumParam={};
    forumParam.UserId=that.data.param.ToUserId;
    forumParam.PageIndex = that.data.page.PageIndex + 1;
    forumParam.PageSize = that.data.page.PageSize;
    app.showLoading();
    httptool.ajaxPost("api/Topic/GetFriendTopic", forumParam, function (res) {
        that.backTopics(res, that);
      },function () { 
          wx.hideLoading(); 
      });
   
  },
  backTopics: function (res,that){//帖子数据处理
    if (res.suc) {
      let data = app.notNull(res.data, []);
      if (data.length > 0) {
        for(let i=0;i<data.length;i++){
          data[i].CreateDate=app.editTime(data[i].CreateDate);
        }
        that.data.page.PageIndex += 1;
        data = that.data.topics.concat(data);
        that.setData({ topics: data, page: that.data.page });
      }
    } else {
       app.showNotify(that, res.msg);
    }
    wx.hideLoading();
  },
  delConcern:function(e){//解除添加关注
    let that=this;
     httptool.ajaxPost("api/Fans/AddOrDeleteFans", that.data.delParam,function(res){
       if(res.suc){
          if(res.data==1){
              wx.showToast({
                title: "关注成功",
                duration: 1000,
                icon: "success"
              });
              let person = that.data.personInfo;
              person.Fans += 1;
              that.setData({ ConConfirm: '取消关注', personInfo: person });
          }else{
             wx.showToast({
                title: "取消成功",
                duration: 1000,
                icon: "success"
              })
              let person = that.data.personInfo;
              person.Fans-=1;
              that.setData({ ConConfirm: '关注', personInfo: person});
          }

       }else{
             app.showNotify(that, res.msg);
          }
     });
  },
  enterForward:function(e){//点击进入转发页面
    let that=this;
    let fourumId=e.currentTarget.dataset.id;
    let Reference=e.currentTarget.dataset.reference;
    if(Reference!=0){
      wx.navigateTo({
        url: '/pages/forward/forward?fourumId='+Reference,
      });
    }else{
      wx.navigateTo({
        url: '/pages/forward/forward?fourumId='+fourumId,
      });
    }
  },
  thumbs_up: function (e) {//点赞事件
    let that = this;
    let isup=e.currentTarget.dataset.isup;
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let likeTimes = e.currentTarget.dataset.liketimes;
    let thumbs = {};
    thumbs.UserId = app.getStorage('customerId');
    thumbs.TopicId = id;
      httptool.ajaxPost("api/TopicZan/AddorDelLike", thumbs, function (res) {
        if (res.suc) {
          if(res.data==1){
            that.data.topics[index].LikeTimes = likeTimes + 1;
            that.setData({ topics: that.data.topics,thumbState:true,zanImg:'../../images/zan_a.png'});
            wx.showToast({
              title: "点赞成功",
              duration: 1000,
              icon: "success"
            })
          }else if(res.data==0){
            that.data.topics[index].LikeTimes = likeTimes - 1;
            that.setData({ topics: that.data.topics,thumbState:false,zanImg:'../../images/zan.png' });
            wx.showToast({
              title: "取消点赞",
              duration: 1000,
              icon: "success"
            })
          }
        } else {
            app.showNotify(that, res.msg);
        }
      });
  },
  enterDetail: function (e) {//点击帖子进入对应的帖子详情
    let forumId = e.currentTarget.dataset.id;
    let UserId=app.getStorage('customerId') || '0';
    wx.navigateTo({
      url: '../detailPost/detailPost?ID=' + forumId+"&UserId="+UserId,
    })
  },
  enterComment:function(e){//点击评论按钮进入对应的帖子详情，并调出回复框
    let forumId = e.currentTarget.dataset.id;
    let UserId=app.getStorage('customerId') || '0';
    wx.navigateTo({
      url: '../detailPost/detailPost?ID=' + forumId+"&dtype="+1+"&UserId="+UserId,
    })
  },
  goToforwardForum:function(e){//点击转发的帖子内容进入对应的帖子详情
    let that=this;
    let forwardId=e.currentTarget.dataset.forumid;
    let retitle=e.currentTarget.dataset.retitle;
    console.log("转发的id是"+forwardId);
    if(forwardId != 0 && retitle != null){
      wx.navigateTo({
        url: '../detailPost/detailPost?ID=' + forwardId +"&redelete="+1,
      });
    }else{
      app.showNotify(that, "原贴内容不存了哦，无法查看！");
     
    }
  },
  previewImg: function (e) {//预览图片
    let imgtype = e.currentTarget.dataset.imgtype;//预览图片类型
    if(imgtype==1){
      let src = e.currentTarget.dataset.src;//获取data-src
      let imgLists = e.currentTarget.dataset.list;//获取data-list
      //图片预览
      wx.previewImage({
        current: src, // 当前显示图片的http链接
        urls: imgLists // 需要预览的图片http链接列表
      })
    } else {
      let url = [];
      url.push(this.data.personInfo.WXQRCode);
      wx.previewImage({
        current: this.data.personInfo.WXQRCode, // 当前显示图片的http链接
        urls: url // 需要预览的图片http链接列表
      })
    }    
  },
  enterDetail: function (e) {//点击帖子进入对应的帖子详情
    let forumId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/detailPost/detailPost?ID=' + forumId,
    })
  },
  checkScore:function(e){//点击查看成绩统计
  let userid=e.currentTarget.dataset.userid;
     wx.navigateTo({
      url: '/pages/myCenter/scores/score?userid='+userid+"&checktype="+1,
    })
  },
})